package com.lordjoe.collectiveintelligence;

import java.text.*;

/**
 * com.lordjoe.collectiveintelligence.svm.LatLon
 *
 * @author Steve Lewis
 * @date May 20, 2009
 */

/**
 * com.lordjoe.gis.LatLon
 * class representing LatLon
 *
 * @author Steve Lewis
 * @date May 6, 2009
 */
public class LatLon
{
    public static LatLon[] EMPTY_ARRAY = {};
    public static Class THIS_CLASS = LatLon.class;

    public static final String LAT_LON_FORMAT = "####.########";

    private final double m_Lat;
    private final double m_Lon;

    public LatLon(double pLat, double pPon)
    {
        m_Lat = pLat;
        m_Lon = pPon;
    }

    public LatLon(String s)
    {
        String[] items = s.split(",");
        m_Lat = Double.parseDouble(items[0]);
        m_Lon = Double.parseDouble(items[1]);

    }

    public double getLat()
    {
        return m_Lat;
    }

    public double getLon()
    {
        return m_Lon;
    }


    public String toString()
    {
        DecimalFormat df = new DecimalFormat(LAT_LON_FORMAT);
        return df.format(getLat()) + "," +
                df.format(getLon());
    }

    public static final int EARTH_RADIUS_KM = 6378;

    /**
     * see http://jan.ucc.nau.edu/~cvm/latlon_formula.html
     *
     * @param ll
     * @return
     */
    public double distanceInKilometers(LatLon ll)
    {
        double a1 = getLat() /57.29577951;
        double b1 = getLon() /57.29577951;
        double a2 = ll.getLat() /57.29577951;
        double b2 = ll.getLon() /57.29577951;

        return Math.acos(Math.cos(a1) * Math.cos(b1) * Math.cos(a2) * Math.cos(b2) +
                Math.cos(a1) * Math.sin(b1) * Math.cos(a2) * Math.sin(b2) +
                Math.sin(a1) * Math.sin(a2)) *
                EARTH_RADIUS_KM;
    }
}
